/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.stylefeng.guns.modular.api;

import com.grape.ciq.clearance.CEB622Message;
import com.grape.ciq.landing.CEB512Message;
import com.grape.ciq.order.CEB312Message;
import com.grape.ciq.pay.CEB412Message;
import com.grape.ciq.support.MarshalUtil;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xml.sax.SAXException;

/**
 *
 * @author wmao
 */
@Controller
@RequestMapping("/api")
public class CiqCallbackController {
    private static final Logger LOG = LoggerFactory.getLogger(CiqCallbackController.class);
    
    @PostMapping(value="/order")
    public void order(String message) {
        try {
            CEB312Message response = (CEB312Message)MarshalUtil.unmarshall(CEB312Message.class, message);
            LOG.info("resp:{}", response);
        } catch (JAXBException | ParserConfigurationException | SAXException ex) {
            LOG.error("failed to unmarshal order message:{}", ex.getMessage());
        }
    }
    
    @PostMapping(value="/clearance")
    @ResponseBody
    public void clearance(String message) {
        try {
            CEB312Message response = (CEB312Message)MarshalUtil.unmarshall(CEB622Message.class, message);
            LOG.info("resp:{}", response);
        } catch (JAXBException | ParserConfigurationException | SAXException ex) {
            LOG.error("failed to unmarshal clearance message:{}", ex.getMessage());
        }
    }
    
    @PostMapping(value="/pay")
    @ResponseBody
    public void pay(String message) {
        try {
            CEB312Message response = (CEB312Message)MarshalUtil.unmarshall(CEB412Message.class, message);
            LOG.info("resp:{}", response);
        } catch (JAXBException | ParserConfigurationException | SAXException ex) {
            LOG.error("failed to unmarshal pay message:{}", ex.getMessage());
        }
    }
    
    @PostMapping(value="/landing")
    @ResponseBody
    public void landing(String message) {
        try {
            CEB312Message response = (CEB312Message)MarshalUtil.unmarshall(CEB512Message.class, message);
            LOG.info("resp:{}", response);
        } catch (JAXBException | ParserConfigurationException | SAXException ex) {
            LOG.error("failed to unmarshal landing message:{}", ex.getMessage());
        }
    }
    
}
